package com.john.http.core.convert;

/**
 * 〈一句话功能简述〉<br/>
 * 〈组合值转换器〉
 *
 * @author jiangguangtao on 2016/5/20.
 */
public class ComboValueConverter implements IValueConverter {
    private IValueConverter[] valueConverters;

    /**
     * 新建一个组合值转换器
     * 将根据传入顺序进行调用转换
     * <p><strong>请保证前面的值转换器转换的值是字符串兼容的，因为后面的值转换器是以String类型的值为参数的</strong></p>
     *
     * @param valueConverters
     */
    public ComboValueConverter(IValueConverter... valueConverters) {
        if (null == valueConverters || valueConverters.length == 0) {
            throw new IllegalArgumentException("请至少输入一个值转换器");
        }
        this.valueConverters = valueConverters;
    }

    @Override
    public String getName() {
        return "组合转换器（虚拟）";
    }

    @Override
    public Object convert(String str) {
        String tstr = str; //临时参数
        Object result = null; //最终结果
        for (IValueConverter vc : valueConverters) {
            Object ret = vc.convert(tstr);
            result = ret;
            if (null != ret) {
                tstr = ret.toString();
            } else {
                tstr = null;
            }

        }

        return result;
    }
}
